﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;

public partial class Popup_Popup_Biz : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetCombo();

            if (!string.IsNullOrEmpty(Request.QueryString["REGNO"]))
            {
                SetBizReg();
                this.tbxBizRegno.ReadOnly = true;
            }
            else
            {
                btnDelete.Visible = false;
            }
        }

        if (!X.IsAjaxRequest)
        {
            //분석 요
            string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. Maecenas tortor turpis, interdum non, sodales non, iaculis ac, lacus. Vestibulum auctor, tortor quis iaculis malesuada, libero lectus bibendum purus, sit amet tincidunt quam turpis vel lacus. In pellentesque nisl non sem. Suspendisse nunc sem, pretium eget, cursus a, fringilla vel, urna.";

            this.ResourceManager1.RegisterClientScriptBlock("text", string.Format("var text=\"{0}\";", text));

            foreach (Portlet portlet in Ext.Net.Utilities.ControlUtils.FindControls<Portlet>(this.Page))
            {
                if (!portlet.ID.Equals("Portlet1"))
                {
                    portlet.Html = "={text}";
                    portlet.Padding = 5;
                }
            }
        }
    }

    private void SetCombo()
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_DDDW_GRCODE_S();

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    ds.Tables[0].Rows[0].Delete();

                    this.StrGRCODE.DataSource = ds;
                    this.StrGRCODE.DataBind();

                    this.cbxGrCode.SelectedIndex = 0;

                    ds = oService.Select_DDDW_CODE_S(ds.Tables[0].Rows[1]["GRCODE"].ToString());
                    StrCODE.DataSource = ds.Tables[0];
                    StrCODE.DataBind();

                    this.cbxCode.SelectedIndex = 0;
                }
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

    protected void ChangeCombo(object sender, DirectEventArgs e)
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            StrCODE.RemoveAll();

            DataSet ds = oService.Select_DDDW_CODE_S(this.cbxGrCode.SelectedItem.Value);
            StrCODE.DataSource = ds.Tables[0];
            StrCODE.DataBind();

            this.cbxCode.SelectedIndex = 0;
        }
    }

    private void SetBizReg()
    {
        try
        {
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_COOPER_1_1_S(Request.QueryString["REGNO"]);

                this.tbxBizRegno.Text = ds.Tables[0].Rows[0]["BIZ_REGNO"].ToString();
                this.cbxGrCode.Value = ds.Tables[0].Rows[0]["GRCODE"];

                StrCODE.RemoveAll();
                DataSet ds2 = oService.Select_DDDW_CODE_S(ds.Tables[0].Rows[0]["GRCODE"].ToString());
                StrCODE.DataSource = ds2.Tables[0];
                StrCODE.DataBind();

                this.cbxCode.Value = ds.Tables[0].Rows[0]["CODE"];


                this.tbxComp_Nm.Text = ds.Tables[0].Rows[0]["COMP_NM"].ToString();
                this.tbxRepr_Nm.Text = ds.Tables[0].Rows[0]["REPR_NM"].ToString();
                this.tbxRepr_ResNo.Text = ds.Tables[0].Rows[0]["REPR_RESNO"].ToString();
                this.tbxTel.Text = ds.Tables[0].Rows[0]["TELNO"].ToString();
                this.tbxFax.Text = ds.Tables[0].Rows[0]["FAXNO"].ToString();
                this.tbxEmail.Text = ds.Tables[0].Rows[0]["E_MAIL_ADDR"].ToString();
                this.tbxAddr.Text = ds.Tables[0].Rows[0]["ADDR"].ToString();
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }


    protected void btnDelete_Click(object sender, DirectEventArgs e)
    {
        try
        {
            int iResult;

            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {

                iResult = oService.DELETE_D_COOPER_1_1_D(this.tbxBizRegno.Text);

                if (!string.IsNullOrEmpty(Request.QueryString["GUBUN"]))
                {
                    iResult = oService.DELETE_D_FJC507T_D(this.tbxBizRegno.Text);
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    protected void btnSave_Click(object sender, DirectEventArgs e)
    {
        try
        {
            int iResult;
            if (string.IsNullOrEmpty(Request.QueryString["REGNO"]))
            {
                using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
                {

                    iResult = oService.INSERT_D_COOPER_1_1_I(this.tbxBizRegno.Text, this.cbxGrCode.SelectedItem.Value, this.cbxCode.SelectedItem.Value
                                                           , this.tbxComp_Nm.Text, this.tbxRepr_Nm.Text, this.tbxTel.Text, this.tbxFax.Text, this.tbxEmail.Text
                                                           , this.tbxRepr_ResNo.Text, this.tbxAddr.Text);

                    if (!string.IsNullOrEmpty(Request.QueryString["GUBUN"]))
                    {
                        iResult = oService.INSERT_D_FJC507T_I(this.tbxBizRegno.Text, " ", " ", " ", "0", " ", "0");
                    }
                }
            }
            else
            {
                using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
                {
                    iResult = oService.UPDATE_D_COOPER_1_1_U(this.tbxBizRegno.Text, this.cbxGrCode.SelectedItem.Value, this.cbxCode.SelectedItem.Value
                                                           , this.tbxComp_Nm.Text, this.tbxRepr_Nm.Text, this.tbxTel.Text, this.tbxFax.Text, this.tbxEmail.Text
                                                           , this.tbxRepr_ResNo.Text, this.tbxAddr.Text);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
